﻿namespace Northwind.Silverlight.Caliburn.Products
{
    using System;
    using System.ComponentModel.Composition;
    using System.Linq;
    using Framework;
    using Csla;

    using Northwind.BusinessObjects; //TODO: Drop server from namespace

    [Export(typeof(IWorkspace))]
    public class ProductsWorkspaceViewModel : DocumentWorkspace<ProductViewModel>
    {
        static int count = 1;
        readonly Func<ProductViewModel> createProductViewModel;

        [ImportingConstructor]
        public ProductsWorkspaceViewModel(Func<ProductViewModel> productVMFactory) {
            createProductViewModel = productVMFactory;
        }

        [Import]
        public IDialogManager Dialogs { get; set; }
        
        public override string IconName
        {
            get { return "Products"; }
        }

        public override string Icon {
            get { return "../Products/Resources/Images/products48.png"; }
        }

        public void New() {
            var vm = createProductViewModel();
            vm.DisplayName = "Product " + count++;
            vm.IsDirty = true;
            Edit(vm);
        }

        public void Go()
        {
            ProductList.GetProductList((o, result) => {
                if (result.Error == null)
                {
                    ProductList products = result.Object;
                    Items.AddRange(products.Select(p => new ProductViewModel(p)));
                }
                else
                {

                }
            });
        }
    }
}